﻿
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace LS.UnityExtensions.Editor
{
    /// <summary>
    /// 类型相关的实用函数。
    /// </summary>
    public static class Type
    {
        private static readonly string[] RuntimeAssemblyNames =
        {
            "Assembly-CSharp",
        };

        private static readonly string[] EditorAssemblyNames =
        {
            "Assembly-CSharp-Editor",
        };

        private static TypeSearch s_TypeSearch = new TypeSearch();

        /// <summary>
        /// 在运行时程序集中获取指定基类的所有子类的名称。
        /// </summary>
        /// <param name="typeBase">基类类型。</param>
        /// <returns>指定基类的所有子类的名称。</returns>
        public static string[] GetRuntimeTypeNames(System.Type typeBase)
        {
            s_TypeSearch.Clear();
            s_TypeSearch.AddAssemblyName(RuntimeAssemblyNames);
            var setting = UnityExtensionsEditorConfig.Instance;
            if (setting != null)
            {
                s_TypeSearch.AddAssemblyDefinition(setting.RuntimeSearchedScopeDefinitions.ToArray());
            }
            return GetTypeNames(s_TypeSearch, typeBase);
        }

        public static string[] GetEditorTypeNames(System.Type typeBase)
        {
            s_TypeSearch.Clear();
            s_TypeSearch.AddAssemblyName(EditorAssemblyNames);
            var setting = UnityExtensionsEditorConfig.Instance;
            if (setting != null)
            {
                s_TypeSearch.AddAssemblyDefinition(setting.EditorSearchedScopeDefinitions.ToArray());
            }
            return GetTypeNames(s_TypeSearch, typeBase);
        }
        
        /// <summary>
        /// 在运行时或编辑器程序集中获取指定基类的所有子类的名称。
        /// </summary>
        /// <param name="typeBase">基类类型。</param>
        /// <returns>指定基类的所有子类的名称。</returns>
        public static string[] GetRuntimeOrEditorTypeNames(System.Type typeBase)
        {
            s_TypeSearch.Clear();
            s_TypeSearch.AddAssemblyName(RuntimeAssemblyNames);
            s_TypeSearch.AddAssemblyName(EditorAssemblyNames);
            var setting = UnityExtensionsEditorConfig.Instance;
            if (setting != null)
            {
                s_TypeSearch.AddAssemblyDefinition(setting.RuntimeSearchedScopeDefinitions.ToArray());
                s_TypeSearch.AddAssemblyDefinition(setting.EditorSearchedScopeDefinitions.ToArray());
            }
            return GetTypeNames(s_TypeSearch, typeBase);
        }

        private static string[] GetTypeNames(TypeSearch typeSearch,System.Type typeBase)
        {
            List<string> typeNames = 
                typeSearch
                .Search((t) => t.IsClass && !t.IsAbstract && typeBase.IsAssignableFrom(t))
                .Select(t=>t.FullName)
                .ToList();
            typeNames.Sort();
            return typeNames.ToArray();
        }
        
                
        /// <summary>
        /// 在运行时程序集中获取指定基类的所有子类类型。
        /// </summary>
        /// <param name="typeBase">基类类型。</param>
        /// <returns>指定基类的所有子类的名称。</returns>
        public static System.Type[] GetRuntimeTypes(System.Type typeBase)
        {
            s_TypeSearch.Clear();
            s_TypeSearch.AddAssemblyName(RuntimeAssemblyNames);
            var setting = UnityExtensionsEditorConfig.Instance;
            if (setting != null)
            {
                s_TypeSearch.AddAssemblyDefinition(setting.RuntimeSearchedScopeDefinitions.ToArray());
            }
            return GetTypes(s_TypeSearch, typeBase);
        }
        
        public static System.Type[] GetEditorTypes(System.Type typeBase)
        {
            s_TypeSearch.Clear();
            s_TypeSearch.AddAssemblyName(EditorAssemblyNames);
            var setting = UnityExtensionsEditorConfig.Instance;
            if (setting != null)
            {
                s_TypeSearch.AddAssemblyDefinition(setting.EditorSearchedScopeDefinitions.ToArray());
            }
            return GetTypes(s_TypeSearch, typeBase);
        }
        
        public static System.Type[] GetRuntimeAndEditorTypes(System.Type typeBase)
        {
            s_TypeSearch.Clear();
            s_TypeSearch.AddAssemblyName(RuntimeAssemblyNames);
            s_TypeSearch.AddAssemblyName(EditorAssemblyNames);
            var setting = UnityExtensionsEditorConfig.Instance;
            if (setting != null)
            {
                s_TypeSearch.AddAssemblyDefinition(setting.RuntimeSearchedScopeDefinitions.ToArray());
                s_TypeSearch.AddAssemblyDefinition(setting.EditorSearchedScopeDefinitions.ToArray());
            }
            return GetTypes(s_TypeSearch, typeBase);
        }
        
        private static System.Type[] GetTypes(TypeSearch typeSearch, System.Type typeBase)
        {
            List<System.Type> types = 
                typeSearch
                    .Search((t) => t.IsClass && !t.IsAbstract && typeBase.IsAssignableFrom(t))
                    .ToList();
            types.Sort((_1, _2) =>
            {
                return _1.Name.CompareTo(_2.Name);
            });
            return types.ToArray();
        }
    }
}
